/*
 * @Author: kangchao kangchao@sxdata.com.cn
 * @Date: 2024-11-21 19:10:26
 * @LastEditors: kangchao kangchao@sxdata.com.cn
 * @LastEditTime: 2024-11-22 17:36:14
 * @FilePath: \daozu\src\store\index.js
 * @Description: 
 * 
 */
//vuex：store.js文件内容如下
import Vue from 'vue'
import Vuex from 'vuex'
import theme from '@/assets/css/theme.scss'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
import { loginApi, paramsConfigApi } from '@/api/index.js'
Vue.use(Vuex)
// import variables from '@/assets/css/element-variables.scss'
export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState({
    storage: window.sessionStorage
  })],
  state: {
    token: "",
    userInfo: {},
    companyConfig: {},
    sizeMeet: {},
    theme: theme,
    adsieWidth: '200px',
    yjData:[], //原卷数据
    cpData:[],//成品数据
    orderWeight:'', //订单中
    yield:'', //成材率
orderInfo:{}
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
    },
    SET_USERINFO(state, userInfo) {
      state.userInfo = userInfo
    },
    SET_COMPANY_CONFIG(state, config) {
      state.companyConfig = config
    },
    SET_SIZE_MEET(state, size) {
      state.sizeMeet = size
    },
    SET_THEME(state, theme) {
      state.theme = theme
    },
    SET_ADSIEWIDTH(state, width) {
      state.adsieWidth = width
    },
    SET_YJDATA(state, data) {
      state.yjData = data
    },
    SET_CPDATA(state, data) {
      state.cpData = data
    },
    SET_ORDER_WEIGHT(state, weight) {
      state.orderWeight = weight
    },
    SET_YIEID(state, data) {
      state.yield = data
    },
    SET_ORDERINFO(state,info){
      state.orderInfo = info
    }
  },
  actions: {
    //设置主题色
    changeSetting({ commit }, data) {
      commit('SET_THEME', data)
    },
    async login({ commit }, data) {
      const res = await loginApi(data)
      if (res.code == 200 && res.data && res.data.access_token) {
        commit('SET_TOKEN', res.data.access_token)
        commit('SET_USERINFO', res.data.client)
        return res
      } else {
        return {
          status: 'error',
          message: res.message
        }
      }

    },

    //获取参数
    getConfig({ commit }) {
      return new Promise((resolve, reject) => {
        paramsConfigApi().then(res => {
          commit("SET_COMPANY_CONFIG", res.data)
          resolve(res)
        }).catch(err => reject(err))
      })
    },
    logout({ commit }) {
      commit('SET_TOKEN', '')
      commit('SET_USERINFO', {})
      commit('SET_COMPANY_CONFIG', {})
    }
  }
})
